package com.HuiShou.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.HuiShou.domain.RecycleOrder;
import com.HuiShou.mapper.RecycleOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.HuiShou.mapper.CollectorOrderMapper;
import com.HuiShou.domain.CollectorOrder;
import com.HuiShou.service.ICollectorOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 接取订单Service业务层处理
 * 
 * @author lyz
 * @date 2025-03-20
 */
@Service
public class CollectorOrderServiceImpl implements ICollectorOrderService 
{
    @Autowired
    private CollectorOrderMapper collectorOrderMapper;
    @Autowired
    private RecycleOrderMapper recycleOrderMapper;  // 假设需要通过订单ID获取订单详情
    @Autowired
    private PointsService pointsService;

    /**
     * 查询接取订单
     * 
     * @param orderId 接取订单主键
     * @return 接取订单
     */
    @Override
    public CollectorOrder selectCollectorOrderByOrderId(String orderId)
    {
        return collectorOrderMapper.selectCollectorOrderByOrderId(orderId);
    }

    /**
     * 查询接取订单列表
     * 
     * @param collectorOrder 接取订单
     * @return 接取订单
     */
    @Override
    public List<CollectorOrder> selectCollectorOrderList(CollectorOrder collectorOrder)
    {
        return collectorOrderMapper.selectCollectorOrderList(collectorOrder);
    }

    /**
     * 新增接取订单
     * 
     * @param collectorOrder 接取订单
     * @return 结果
     */
    @Override
    public int insertCollectorOrder(CollectorOrder collectorOrder)
    {
        return collectorOrderMapper.insertCollectorOrder(collectorOrder);
    }

    /**
     * 修改接取订单
     * 
     * @param collectorOrder 接取订单
     * @return 结果
     */
    @Override
    public int updateCollectorOrder(CollectorOrder collectorOrder)
    {
        return collectorOrderMapper.updateCollectorOrder(collectorOrder);
    }

    /**
     * 批量删除接取订单
     * 
     * @param orderIds 需要删除的接取订单主键
     * @return 结果
     */
    @Override
    public int deleteCollectorOrderByOrderIds(String[] orderIds)
    {
        return collectorOrderMapper.deleteCollectorOrderByOrderIds(orderIds);
    }

    /**
     * 完成接取订单
     * 
     * @param collectorOrder 接取订单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)  // 添加事务管理
    public int finishCollectorOrder(CollectorOrder collectorOrder)
    {
        // 1. 获取原始订单状态（确保状态合法性）
        RecycleOrder originalOrder = recycleOrderMapper.selectByIdD(collectorOrder.getOrderId());
        if (originalOrder == null) {
            throw new RuntimeException("订单不存在");
        }
        if (!originalOrder.getStatus().equals("accepted")) {  // 仅允许从"已接单"状态变为"已完成"
            throw new RuntimeException("订单当前状态不可完成");
        }

        // 2. 更新订单状态为completed
        int updateCount = collectorOrderMapper.updateCollectorOrder(collectorOrder);
        if (updateCount == 0) {
            throw new RuntimeException("订单更新失败");
        }

        // 3. 获取订单完整数据（包含total_value）
        RecycleOrder completedOrder = recycleOrderMapper.selectByIdD(collectorOrder.getOrderId());

        // 4. 发放积分（需确保total_value不为空）
        if (completedOrder.getTotalValue() == null || completedOrder.getTotalValue().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("订单价值异常，无法发放积分");
        }
        pointsService.addPoints(
                completedOrder.getClientId(),
                completedOrder.getTotalValue(),
                "order",
                completedOrder.getOrderNo()
        );

        return updateCount;
    }
}
